using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using umbraco.interfaces;
using IAS.MacroRenderings.Interfaces;

namespace IAS.MacroRenderings.Data
{
    /// <summary>
    /// Your usedefined macro rendering type needs to inherit from this class so that it behaves
    /// like a dropdown and can be read by umbraco via the IMacroGuiRendering interface
    /// </summary>
    public abstract class BaseDataProvider : DropDownList, IMacroGuiRendering
    {

        #region IMacroGuiRendering Members
        string _value = String.Empty;
        
        public bool ShowCaption
        {
            get { return true; }
        }

        public string Value
        {
            get
            {
                string retval = String.Empty;
                foreach (ListItem li in base.Items)
                {
                    if (li.Selected)
                    {
                        retval += li.Value + ",";
                    }
                }
                if (retval != string.Empty)
                {
                    retval = retval.TrimEnd(new char[] { ',' });
                }
                return retval;
            }
            set
            {
                _value = value;
            }

        }

        #endregion

        /// <summary>
        /// this will contain the data from the provider
        /// </summary>
        protected IDropDownListDataProvider DropDownData;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "guiInputTextStandard";
            this.Items.Add(new ListItem("", ""));          
        }
        
        protected void fillComboData() {

            if (DropDownData != null)
            {
                // this hack maps class name to a file name could be csv / xml file with your drop down data
                DropDownData.ClassName = this.GetType().FullName;
                
                Dictionary<string, string> dt = DropDownData.GetData();
                foreach (KeyValuePair<string, string> kvp in dt)
                {
                    this.Items.Add(new ListItem(kvp.Value, kvp.Key));
                }
            }
        }

        /// <summary>
        /// must implement this in your derived class
        /// </summary>
        abstract protected void fillCombo();
    }
}
